package com.songlanyun.modules.comment.controller.admin;

import java.util.Arrays;
import java.util.Map;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.comment.entity.OrderGoodsComment;
import com.songlanyun.modules.comment.entity.OrderGoodsCommentReply;
import com.songlanyun.modules.comment.service.OrderGoodsCommentReplyService;
import com.songlanyun.modules.comment.service.OrderGoodsCommentService;
import com.songlanyun.modules.exception.ShopException;
import com.songlanyun.modules.sensitive.service.SystemSensitiveWordService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;

import javax.annotation.Resource;


/**
 * 订单商品评论回复
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-30 18:09:47
 */
@RestController
@RequestMapping("admin/comment/orderGoods/reply")
public class OrderGoodsCommentReplyAdminController extends AbstractController {
    @Autowired
    private OrderGoodsCommentReplyService orderGoodsCommentReplyService;

    @Resource
    private OrderGoodsCommentService orderGoodsCommentService;

    @Resource
    private SystemSensitiveWordService systemSensitiveWordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("comment:orderGoodsCommentReply:list")
    public R list(@RequestParam Map<String, Object> params) {
        setShopId(params);
        PageUtils page = orderGoodsCommentReplyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("comment:orderGoodsCommentReply:info")
    public R info(@PathVariable("id") Long id) {
        OrderGoodsCommentReply orderGoodsCommentReply = orderGoodsCommentReplyService.getById(id);

        return R.ok().put("data", orderGoodsCommentReply);
    }

    /**
     * 评论回复
     */
    @SysLog("订单商品评论回复")
    @RepeatSubmit("2")
    @PostMapping("/save")
//    @RequiresPermissions("comment:orderGoodsCommentReply:save")
    public R save(@RequestBody OrderGoodsCommentReply orderGoodsCommentReply) {
        ValidatorUtils.validateEntity(orderGoodsCommentReply);

        //过滤敏感词
        String filter = systemSensitiveWordService.filter(orderGoodsCommentReply.getContent());
        orderGoodsCommentReply.setContent(filter);
        //校验商家
        OrderGoodsComment comment = orderGoodsCommentService.getById(orderGoodsCommentReply.getCommentId(), true);
        verifyShopId(comment.getShopId());

        orderGoodsCommentReplyService.saveReply(orderGoodsCommentReply,comment);

        return R.ok();
    }


    /**
     * 删除
     */
    @SysLog("订单商品评论删除")
    @DeleteMapping("/delete/{id}")
//    @RequiresPermissions("comment:orderGoodsCommentReply:delete")
    public R delete(@PathVariable("id") Long id) {
        //校验商家
        OrderGoodsComment comment = orderGoodsCommentService.getById(id, true);
        verifyShopId(comment.getShopId());
        orderGoodsCommentReplyService.removeById(id);

        return R.ok();
    }


}
